home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte16 / chkclrig.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  9KB  |  256 lines

  1.  
  2. #include <windows.h>  
  3. #include "ChkClrIG.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    switch( uMsg )
  110.    {
  111.       case WM_COMMAND :
  112.               switch( LOWORD( wParam ) )
  113.               {
  114.                  case IDM_TEST :
  115.                        {
  116.                           LOGCOLORSPACE ColorSpace;
  117.                           HCOLORSPACE   hColorSpace;
  118.                           RGBQUAD       aRGB[5];
  119.                           BYTE          aResults[5];
  120.                           HBRUSH        hBrush;
  121.                           int           i;
  122.                           HDC           hDC;
  123.                           char          szMsg[30];
  124.                           BOOL          bRet;
  125.                            
  126.                           // Get a DC for the printer.
  127.                           //..........................
  128.                           hDC = CreateDC( "WINSPOOL", 
  129.                                           "HP DeskJet 550C Printer", 
  130.                                           NULL, NULL );
  131.  
  132.                           // Turn on Image Color Matching.
  133.                           //..............................
  134.                           SetICMMode( hDC, ICM_ON );
  135.  
  136.                           // Load color array to test.
  137.                           //..........................
  138.                           for( i = 0; i < 5; i++ )
  139.                           {
  140.                              aRGB[i].rgbRed   = (i > 0 ? (255 / i) : 0 );
  141.                              aRGB[i].rgbGreen = (i > 0 ? (255 / i) : 0 );
  142.                              aRGB[i].rgbBlue  = (i > 0 ? (255 / i) : 0 );
  143.                              aRGB[i].rgbReserved = 0;
  144.                               aResults[i] = 0;
  145.                           }
  146.  
  147.                           // Fill a structure to create a color space.
  148.                           //..........................................
  149.                           memset( &ColorSpace, 0, sizeof( ColorSpace ) );
  150.                           ColorSpace.lcsSignature=(DWORD)0x50534F43;
  151.                           ColorSpace.lcsVersion=0x00000400;
  152.                           ColorSpace.lcsSize=sizeof( ColorSpace );
  153.                           ColorSpace.lcsCSType=LCS_DEVICE_RGB;
  154.                           ColorSpace.lcsIntent=LCS_GM_BUSINESS;
  155.  
  156.                           // Create a color space.
  157.                           //......................
  158.                           hColorSpace = CreateColorSpace( &ColorSpace );
  159.                           SetColorSpace( hDC, hColorSpace );
  160.                           GetLastError();
  161.  
  162.                           // Check the colors in the gamut.
  163.                           //...............................
  164.                           bRet = CheckColorsInGamut( hDC, aRGB, aResults, 5 ); 
  165.  
  166.                           DeleteColorSpace( hColorSpace );
  167.                           SetICMMode( hDC, ICM_OFF );
  168.                           DeleteDC( hDC );
  169.  
  170.                           // Display the results.
  171.                           //.....................
  172.                           if ( bRet )
  173.                           {
  174.                              hDC = GetDC( hWnd );
  175.  
  176.                              for( i = 0; i < 5; i++ )
  177.                              {
  178.                                 hBrush = CreateSolidBrush( 
  179.                                                    RGB( aRGB[i].rgbRed,
  180.                                                         aRGB[i].rgbGreen,
  181.                                                         aRGB[i].rgbBlue ) );
  182.                                 SelectObject( hDC, hBrush );
  183.  
  184.                                 Rectangle( hDC, 10, (i*30)+10, 
  185.                                                 40, (i*30)+40 );
  186.  
  187.                                 wsprintf( szMsg, "RGB( %d, %d, %d )",
  188.                                                  aRGB[i].rgbRed,
  189.                                                  aRGB[i].rgbGreen,
  190.                                                  aRGB[i].rgbBlue );
  191.  
  192.                                 TextOut( hDC, 50, (i*30)+20, szMsg, 
  193.                                                   strlen( szMsg ) );
  194.  
  195.                                 if ( aResults[i] == CM_OUT_OF_GAMUT )
  196.                                    TextOut( hDC, 200, (i*30)+20, 
  197.                                                  "- Not in the Gamut.", 19 );
  198.                                 else
  199.                                    TextOut( hDC, 200, (i*30)+20, 
  200.                                                  "- In the Gamut.", 15 );
  201.  
  202.                                 SelectObject( hDC, 
  203.                                               GetStockObject( WHITE_BRUSH ) );
  204.                                 DeleteObject( hBrush );
  205.                              } 
  206.  
  207.                              ReleaseDC( hWnd, hDC );
  208.                           }
  209.                        }
  210.                        break;
  211.  
  212.                  case IDM_ABOUT :
  213.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  214.                         break;
  215.  
  216.                  case IDM_EXIT :
  217.                         DestroyWindow( hWnd );
  218.                         break;
  219.               }
  220.               break;
  221.       
  222.       case WM_DESTROY :
  223.               PostQuitMessage(0);
  224.               break;
  225.  
  226.       default :
  227.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  228.    }
  229.  
  230.    return( 0L );
  231. }
  232.  
  233.  
  234. LRESULT CALLBACK About( HWND hDlg,           
  235.                         UINT message,        
  236.                         WPARAM wParam,       
  237.                         LPARAM lParam)
  238. {
  239.    switch (message) 
  240.    {
  241.        case WM_INITDIALOG: 
  242.                return (TRUE);
  243.  
  244.        case WM_COMMAND:                              
  245.                if (   LOWORD(wParam) == IDOK         
  246.                    || LOWORD(wParam) == IDCANCEL)    
  247.                {
  248.                        EndDialog(hDlg, TRUE);        
  249.                        return (TRUE);
  250.                }
  251.                break;
  252.    }
  253.  
  254.    return (FALSE); 
  255. }
  256.